//
//  scrollowVC.m
//  Notification
//
//  Created by Apple on 2017/3/22.
//  Copyright © 2017年 Apple. All rights reserved.
//
#import "HongFile.pch"
#import "scrollowVC.h"

@interface scrollowVC ()<UIScrollViewDelegate>
@property(nonatomic,strong)NSTimer*tim;
@end

@implementation scrollowVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"ScrollowVC";
    self.view.backgroundColor = [UIColor orangeColor];
    
    UILabel*laber = [[UILabel alloc]initWithFrame:[UIScreen mainScreen].bounds];
    laber.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:laber];
#pragma mark-初始化
    self.scro = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 200, SCREEN_WIDTH, 200)];
#pragma mark-设置滚动范围
    self.scro.contentSize = CGSizeMake(SCREEN_WIDTH*3, 200);
#pragma mark-设置分页效果
    self.scro.pagingEnabled = YES;
#pragma mark-设置水平滚动条隐藏
    self.scro.showsHorizontalScrollIndicator = NO;
    self.scro.showsVerticalScrollIndicator = NO;
    self.scro.bounces = NO;
    [self.view addSubview:self.scro];
#pragma mark-设置添加3个子视图
    for (int i = 0; i<3; i++) {
        UIImageView*sub = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*i,0, SCREEN_WIDTH, self.scro.frame.size.height)];
        sub.backgroundColor = [UIColor whiteColor];
        sub.image = [UIImage imageNamed:[NSString stringWithFormat:@"picture%d.jpg",i]];
        sub.layer.borderWidth = 1;
        sub.layer.borderColor = [UIColor blackColor].CGColor;
        [self.scro addSubview:sub];
    }
#pragma mark-为滚动试图右边添加一个试图，让它和第一个试图一样
    UIImageView*lab = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*3,0, SCREEN_WIDTH, self.scro.frame.size.height)];
    lab.backgroundColor = [UIColor whiteColor];
    lab.layer.borderWidth = 1;
    lab.layer.borderColor = [UIColor blackColor].CGColor;
    lab.image = [UIImage imageNamed:@"picture0.jpg"];
    [self.scro addSubview:lab];
    self.scro.delegate = self;
    
    self.tim = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(change) userInfo:nil repeats:YES];
    
    
}
#pragma mark-开始拖拽的代理方法，在此方法中暂停定时器
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
#pragma mark-在将来的某一刻启动
    [self.tim setFireDate:[NSDate distantFuture]];
}
#pragma mark-试图静止时，开启定时器，自动轮播
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
#pragma mark-试图静止后，过1.5秒开始轮播
    [self.tim setFireDate:[NSDate dateWithTimeInterval:1.5 sinceDate:[NSDate date]]];
}
#pragma mark-定时器的回调方法
-(void)change{
#pragma mark-先获得此时的偏移量
    float x = self.scro.contentOffset.x;
#pragma mark-每次切换一个偏移量
    x += SCREEN_WIDTH;
    
#pragma mark-如果判断是最右边的试图，就改变第一个试图偏移量
    if (x > SCREEN_WIDTH*3) {
        self.scro.contentOffset = CGPointMake(0, 0);
        [self.scro setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:YES];
    }else{
        [self.scro setContentOffset:CGPointMake(x,0) animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
